const fs = require("fs");
const fsPromises = require("fs").promises;

const path = require("path");


function deleteFile(path) {
    var files = [];
    if (/@ventose/gi.test(path)) return;
    if (fs.existsSync(path)) {
        files = fs.readdirSync(path);
        files.forEach(function (file, index) {
            var curPath = path + "/" + file;
            console.log("🚀 ~ file: delete_node_modules.js ~ line 13 ~ curPath", curPath);
            if (fs.statSync(curPath).isDirectory()) {
                deleteFile(curPath);
            } else {
                fs.unlinkSync(curPath);
            }
        });
        fs.rmdirSync(path);
    }
};


(async () => {
    const modules = await fsPromises.readdir(path.resolve("./packages"));
    modules.forEach(async dir_name => {
        ['package-lock.json', 'node_modules'].forEach(async targetName => {
            const curPath = path.resolve("./packages", dir_name, targetName);
            try {
                deleteFile(curPath);
            } catch (error) {
                console.log(`error : /${error}`);
            } finally {
                console.log(`current : ${curPath}`)
            }
        })
    })
})();